<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ReduceModel
 *
 * @author 敏学
 */
class ReduceModel extends AdjustPublicModel {

    protected $tableName = 'adjust';
    protected $_validDetail = array(
        array("product_id", 'require', 'require', 1),
        array("warehouse_id", 'require', 'require', 1),
        array("color_id", 'require', 'require', 0),
        array("size_id", 'require', 'require', 0),
        array("quantity", 'currency', 'double', 1),
        array("capability", 'z_integer', 'z_integer', 0),
        array("dozen", 'z_integer', 'z_integer', 0),
        array("price", 'double', 'double', 1),
    );

    /**
     * 业务规则验证
     *
     */
    public function _brf() {
        // 业务规则检查
        $this->_module = 'Adjust';
        $all_tags = C('extends');
        $action_tag_name = $this->_module.'^'.$this->_action;
        if (isset($all_tags[$action_tag_name])) {
            // 添加关联类的模块信息
            $_POST['_module'] = $this->_module;
            $_POST['_action'] = $this->_action;
            $info   = $_POST;
            foreach ($info['detail'] as $key => $val) {
                floatval($val['quantity']) != 0 && $info['detail'][$key]['quantity'] = -$val['quantity'];
            }
            tag($action_tag_name, array_merge($_GET, $info));
        }
    }

    public function _beforeModel($info) {
        $this->_module = 'Adjust';
        foreach ($info['detail'] as $key => $val) {
            floatval($val['quantity']) != 0 && $this->data['detail'][$key]['quantity'] = -$val['quantity'];
        }
        return true;
    }

    /**
     * 所有订单列表SQL
     *
     * @return  array
     */
    function indexSql() {

        $count = $this->exists('select 1 from adjust_detail where adjust_id=adjust.id and quantity<0 and ' . getWhere($_POST['detail']))->where(getWhere($_POST['main']))->count();
        $this->setPage($count);
        $ids = $this->field('id')->exists('select 1 from adjust_detail where adjust_id=adjust.id and quantity<0 and ' . getWhere($_POST['detail']))->where(getWhere($_POST['main']))->order('adjust_no desc')->page()->selectIds();
        $info['from'] = 'adjust a LEFT JOIN adjust_detail b ON a.id = b.adjust_id';
        $info['group'] = ' group by a.id order by adjust_no desc';
        $info['where'] = ' where a.id in' . $ids;
        $info['field'] = 'a.id AS id,a.adjust_no AS adjust_no,a.adjust_date AS adjust_date,a.currency_id AS currency_id,b.adjust_id,b.product_id AS product_id,b.size_id AS size_id,abs(b.quantity) AS quantity,b.capability AS capability,b.warehouse_id AS warehouse_id,sum(abs(quantity)*capability*dozen) AS sum_quantity,sum(abs(quantity)) AS total_quantity,sum(abs(quantity)*capability*dozen*price) AS money';
        return 'select ' . $info['field'] . ' from ' . $info['from'] . $info['where'] . $info['group'];
    }

    /**
     * 获取明细信息(用于查看/编辑)
     *
     * @param int $id
     * @return array
     */
    public function getInfo($id) {
        $rs = $this->find($id);
        $rs['detail'] = M('AdjustDetail')->field('*,abs(quantity) quantity,(abs(quantity)*capability*dozen) AS sum_quantity,(abs(quantity)*capability*dozen*price) AS money')->where('adjust_id=' . $id)->select();
        return _formatListRelation($rs);
    }

}

?>
